<?php
/**
 * Created by PhpStorm.
 * User: CJJ
 * Date: 2017/8/23
 * Time: 13:54
 */

require_once ('../../SQLBase.php');
//require_once ($CDir.'/GongAn/admin/Common.php');
class UserManager{

    //通过姓名搜索用户数据
    function searchUserDataByUserName($userName){
        $db=new SQLBase();
        $queryStr="SELECT user_ID FROM user WHERE user_state=1 AND user_name LIKE '%".$userName."%';";
        $result=$db->queryData($queryStr);
        if($result){
            while($temp=$result->fetch_array()){
                $userIdArray[]=$temp['user_ID'];
            }
            if(count($userIdArray)>0)
                foreach ($userIdArray as $value){
                    $queryStr="SELECT distinct `user`.user_ID,`user`.user_name,executive.name FROM `user`,executive where  `user`.executive_level=executive.level  and user.user_ID=".$value.";";
                    $result=$db->queryData($queryStr);
                    if ($result->num_rows!=0) {
                        $temp=$result->fetch_array();
                        $userData["user_number"]=$temp['user_ID'];
                        $userData["user_name"]=$temp['user_name'];
                        $userData["name"]=$temp['name'];
                        $userDataAll[]=$userData;
                    }
                }
            if($userDataAll!=null)
                return $userDataAll;
            else
                return null;
        }
    }

    //获取所有部门的部门信息
    function getAllSectionData(){
        $db=new SQLBase();
        $queryStr="SELECT * FROM allsection WHERE section_state=1;";
        $result=$db->queryData($queryStr);
        if($result){
            while($temp=$result->fetch_array()){
                $sectionData['section_ID']=$temp['section_ID'];
                $sectionData['section_name']=$temp['section_name'];
                $sectionDataAll[]=$sectionData;
            }
            return $sectionDataAll;
        }
    }

    //通过部门id获取人员数数据
    function getUserDataBySectionId($sectionId){
        $db=new SQLBase();
        $queryStr="SELECT distinct `user`.user_ID,`user`.user_name,allsection.section_name,section.section_job,executive.name FROM `user`,section,allsection,executive where allsection.section_ID=section.section_ID AND`user`.user_ID=section.user_ID AND `user`.executive_level=executive.level  and `section`.section_ID=".$sectionId.";";
        $result=$db->queryData($queryStr);
        if($result){
            while ($temp=$result->fetch_array()){
                $userData["user_number"]=$temp['user_ID'];
                $userData["user_name"]=$temp['user_name'];
                $userData["section_name"]=$temp['section_name'];
                $userData["section_job"]=$temp['section_job'];
                $userData["name"]=$temp['name'];
                $userDataAll[]=$userData;
            }
        }
        return $userDataAll;
    }

    //通过用户ID获取信息
    function getUserData($userNumber){
        $db=new SQLBase();
        $queryStr="SELECT * FROM user WHERE user_ID=".$userNumber.";";
        $result=$db->queryData($queryStr);
        if($result){
            $temp=$result->fetch_array();
            $userData['user_number']=$temp['user_ID'];
            $userData['user_name']=$temp['user_name'];
            $userData['user_pwd']=$temp['user_pwd'];
            $userData['card_number']=$temp['card_number'];
            $userData['executive_level']=$temp['executive_level'];
            return $userData;
        }
    }

    //通过用户名称和部门查找个人信息
    function getUserDataByUserNameAndSectionId($userName,$sectionId){
        $db=new SQLBase();
        $queryStr="SELECT `user`.user_ID,`user`.user_ID,`user`.user_name,executive.`name` FROM  user,section,allsection,executive WHERE user.user_state=1 AND `user`.user_ID=section.user_ID AND allsection.section_ID=section.section_ID AND `user`.executive_level=executive.`level` AND `user`.user_name LIKE '%%".$userName."%%'  AND section.section_ID=".$sectionId.";";
        $result=$db->queryData($queryStr);
        if($result){
            while($temp=$result->fetch_array()){
                $userIdArray[]=$temp['user_ID'];
                // 			echo $temp['user_ID']."<br>";
            }
            if(count($userIdArray)>0)
                foreach ($userIdArray as $value){
                    $queryStr="SELECT distinct `user`.user_ID,`user`.user_name,executive.name FROM `user`,executive where  `user`.executive_level=executive.level  and user.user_number=".$value.";";
                    // 		echo $queryStr."<br>";
                    $result=$db->queryData($queryStr);
                    if ($result->num_rows!=0) {
                        // 			echo $result->num_rows;
                        $temp=$result->fetch_array();
                        $userData["user_number"]=$temp['user_ID'];
                        $userData["user_name"]=$temp['user_name'];
                        // 		$userData["section_name"]=$temp['section_name'];
                        // 		$userData["section_job"]=$temp['section_job'];
                        $userData["name"]=$temp['name'];
                        $userDataAll[]=$userData;
                    }
                }
            if($userDataAll!=null)
                return $userDataAll;
        }
    }

    //分页:获取user数据量,主要给予处理
    function getPageCount(){
        $db=new SQLBase();
        $queryStr="SELECT count(*) FROM user WHERE user_state=1;";
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $temp=$result->fetch_assoc();
            $resultArrayAll['data']=$temp['count(*)'];
        }
        return $resultArrayAll;
    }

    //分页:获取当前页用户
    function getUserDataLimit($n,$m){
        $db=new SQLBase();
        $queryStr="SELECT `user`.user_ID,`user`.user_name,executive.name FROM `user`,executive where  `user`.executive_level=executive.level AND user.user_state=1 Limit ".$n.",".$m.";";
        $result=$db->queryData($queryStr);
        if($result){
            $resultArrayAll['state']=1;
            while ($temp=$result->fetch_array()){
                $userData["user_number"]=$temp['user_ID'];
                $userData["user_name"]=$temp['user_name'];
                $userData["section_name"]=$temp['section_name'];
                $userData["section_job"]=$temp['section_job'];
                $userData["name"]=$temp['name'];
                $resultArrayAll['data'][]=$userData;
            }
        }
        else{
            $resultArrayAll['state']=-1;
        }
        return $resultArrayAll;
    }

    //User添加数据
    function insertUserData($data){
        $db=new SQLBase();
        $queryStr="SELECT user_ID FROM user WHERE user_ID=".$data['user_number'].";";
        $result=$db->queryData($queryStr);
        if(0 == $result->num_rows){
            $queryStr='INSERT INTO user(user_ID,user_name,user_pwd,card_number,executive_level) VALUES(';
            $queryStr.=$data['user_number'].',';
            $queryStr.='"'.$data['user_name'].'",';
            $queryStr.='"'.$data['user_pwd'].'",';
            $queryStr.='"'.$data['card_number'].'",';
            $queryStr.=$data['executive_level'].');';
//            echo $queryStr;
            $result=$db->queryData($queryStr);
            if(!$result){
                $userData['state']=-1;
            }else{
                $userData['state']=1;
            }
        }else{
            $userData['state']=-1;
        }
        return $userData;
    }

    //更新User数据
    function updateUserData($data){
        $db=new SQLBase();
        $queryStr='UPDATE user SET ';
        $queryStr.='user_name="'.$data['user_name'].'",';
        $queryStr.='user_pwd="'.$data['user_pwd'].'",';
        $queryStr.='card_number='.$data['card_number'].',';
        $queryStr.='executive_level='.$data['executive_level'].' WHERE ';
        $queryStr.='user_ID='.$data['user_number'].';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $userData['state']=-1;
        }else{
            $userData['state']=1;
        }
        return $userData;
    }

    //删除User数据
    function deleteUserData($user_number){
        $db=new SQLBase();
        $queryStr='SELECT user_ID FROM user WHERE user_ID='.$user_number.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $userData['state']=-1;
        }else{
            $temp=$result->fetch_array();
            $queryStr='UPDATE user SET user_state=0 WHERE user_ID='.$user_number.';';
            $result=$db->queryData($queryStr);
            if($result){
                $queryStr='DELETE FROM section WHERE user_ID='.$temp['user_ID'].';';
                $result=$db->queryData($queryStr);
                if($result)
                    $userData['state']=1;
            }
        }
        return $userData;
    }
}